package com.wis.bolt.mpg;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wis.TopologyDrive;
import com.wis.pool.redisCluster.MyJedisClusterPool;
import com.wis.tools.CommonUtiles;
import com.wis.tools.MPGUtiles;
import org.apache.commons.lang.StringUtils;
import org.apache.storm.task.OutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @Description: traffic_mpg_two    支撑产品的卡值计算
 * @author: fan
 * @Date: Created in 2018/12/3 11:05
 * @Modified By:
 */
public class MPGKaZhiBolt extends BaseRichBolt {
    private OutputCollector collector;
    private static MPGUtiles utiles;
    private static JSONArray jsonKaZhi;
    private static String typeProduce;
    private static boolean flag;
    private static Map map;
    private static Map<String, String> kaZhiProMap;
    private static List list;
    private static JedisCluster jedisCluster = null;

    @Override
    public void prepare(Map map, TopologyContext context, OutputCollector collector) {
        this.collector = collector;
        utiles = new MPGUtiles();
        this.collector = collector;
        this.map = map;
        kaZhiProMap = new HashMap<>();
        list = new LinkedList();
        typeProduce = map.get(TopologyDrive.TYPE_PRODUCE).toString();
        flag = true;
        // 获取默认的指标卡值数据
        try {
            if (jedisCluster == null) jedisCluster = MyJedisClusterPool.getRedisClusterObject();
            jsonKaZhi = JSONArray.parseArray(CommonUtiles.mapToJson(jedisCluster.hget(map.get(TopologyDrive.REDIS_MPG_KAZHI).toString(), "100000")));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyJedisClusterPool.returnJedisCluterObject(jedisCluster);
        }
    }

    @Override
    public void execute(Tuple tuple) {
        String key = tuple.getStringByField("key");
        // 能见度 风速 风向-- 降水量 将水相态 气温 相对湿度 道路结冰
        String njd = tuple.getStringByField("nengjiandu");
        String id = tuple.getStringByField("id");

        try {
            if (njd.equals("over")) {
                this.collector.emit(new Values(key, "over", "over", "over", "over", "over", "over", "over", "over"));
            } else if (njd.equals("jies")) {
                this.collector.emit(new Values(key, "jies", "jies", "jies", "jies", "jies", "jies", "jies", "jies"));
                flag = true;
            } else if (StringUtils.isNotBlank(njd)) {
                if (flag) {
                    try {
                        if (jedisCluster == null) jedisCluster = MyJedisClusterPool.getRedisClusterObject();
                        if (jedisCluster.exists(map.get(TopologyDrive.REDIS_MPG_KAZHI).toString())) {
                            jsonKaZhi = JSON.parseArray(CommonUtiles.mapToJson(jedisCluster.hget(map.get(TopologyDrive.REDIS_MPG_KAZHI).toString(), "100000")));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        MyJedisClusterPool.returnJedisCluterObject(jedisCluster);
                        flag = false;
                    }
                }
                String jsxt = tuple.getStringByField("jiangshuixiangtai");
                String nengjiandu = utiles.vBKaZhi(jsonKaZhi.getJSONObject(0), njd);
                String fengsu = utiles.commonKaZhi(jsonKaZhi.getJSONObject(1), tuple.getStringByField("fengsu"));
                String qiwen = utiles.commonKaZhi(jsonKaZhi.getJSONObject(6), tuple.getStringByField("qiwen"));
                String xdsd = utiles.commonKaZhi(jsonKaZhi.getJSONObject(3), tuple.getStringByField("xiangduishidu"));
                String jiebing = utiles.jieBingNewKaZhi(jsonKaZhi.getJSONObject(2), tuple.getStringByField("daolujiebing"));

                String[] jsxts = jsxt.split(",", -1);
                StringBuffer stringBuffer = new StringBuffer();
                String[] jsls = tuple.getStringByField("jiangshuiliang").split(",", -1);
                for (int i = 0; i < jsxts.length; i++) {
                    if (i == 0 && jsxts.length > 5) continue;
                    // 雨夹雪-->雨
                    if (jsxts[i].contains("301") || jsxts[i].contains("6")) {
                        stringBuffer.append(utiles.commonKaZhi(jsonKaZhi.getJSONObject(4), jsls[i]));
                    } else if (jsxts[i].contains("302")) {
                        stringBuffer.append(utiles.commonKaZhi(jsonKaZhi.getJSONObject(5), jsls[i]));
                    } else {
                        stringBuffer.append("#ffffff" + "_" + jsls[i]);
                    }
                    stringBuffer.append(",");
                }
                String jiangshuiliang = stringBuffer.substring(0, stringBuffer.length() - 1).toString();
                this.collector.emit(new Values(key, id, nengjiandu, fengsu, jiangshuiliang, jsxt, qiwen, xdsd, jiebing));

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.collector.ack(tuple);
        }
    }

    @Override
    public void declareOutputFields(OutputFieldsDeclarer declarer) {
        declarer.declare(new Fields("key", "id", "nengjiandu", "fengsu", "jiangshuiliang", "jiangshuixiangtai", "qiwen", "xiangduishidu", "daolujiebing"));
    }
}
